<!doctype html>
  <meta charset="utf-8">
  <link rel="stylesheet" type="text/css" href="../node_modules/mocha/mocha.css">
  <script src="../node_modules/mocha/mocha.js"></script>
  <script src="../node_modules/when/es6-shim/Promise.js"></script>
  <script src="lib/systemjs-020/system.js" type="text/javascript"></script>
  <body>
    <div id="mocha">
  <script>
    DEBUG = false;

    mocha.setup('tdd');

    function ok(assertion, msg) {
      if (!assertion)
        throw new Error(msg || 'Assertion failed');
    }

    System.config({
      baseURL: './fixtures/test-tree/',
      meta: {
         'output/*': { format: 'register' }
      },
      paths: {
        'traceur-runtime': '../node_modules/traceur/bin/traceur-runtime.js',
        'babel/external-helpers': '../node_modules/babel-core/external-helpers.js',
        'jquery-cdn': './jquery.js',
        'output/': './output/'
      },
      bundles: {
        'output/tree-build.js': ['third.js', 'cjs.js', 'jquery.js', 'second.js', 'global.js', 'amd.js', 'first.js'],
        'output/umd.js': ['umd.js']
      }
    });

    suite('Test Tree Bundle', function () {
      test('Loading AMD from a bundle', function(done) {
        System['import']('amd.js').then(function(m) {
          ok(m.is == 'amd');
          ok(m.text == 'This is some text');
          done();
        })
        .catch(done);
      });

      test('Loading CommonJS from a bundle', function(done) {
        System['import']('cjs.js').then(function(m) {
          ok(m.env == 'production');
          ok(m.cjs === true);
          done();
        })
        .catch(done);
      });

      test('Loading a Global from a bundle', function(done) {
        System['import']('global.js').then(function(m) {
          ok(m === 'output');
          done();
        })
        .catch(done);
      });

      test('Loading named System.register', function(done) {
        System['import']('third.js').then(function(m) {
          ok(m.some == 'exports');
          ok(m.pi == 'π');
          done();
        })
        .catch(done);
      });

      test('Loading System.register from ES6', function(done) {
        this.timeout(10000);
        System['import']('first.js').then(function(m) {
          ok(m.p == 5);
          done();
        })
        .catch(done);
      });

      test('AMD rewriting 1', function(done) {
        System['import']('output/amd-1.js').then(function(m) {
          return System['import']('amd-1.js');
        })
        .then(function(m) {
          ok(m.first.p == 5);
          done();
        })
        .catch(done);
      });

      test('AMD rewriting 2', function(done) {
        System['import']('output/amd-2.js').then(function(m) {
          return System['import']('amd-2.js');
        })
        .then(function(m) {
          ok(m.amd == 2);
          done();
        })
        .catch(done);
      });

      test('AMD rewriting 3', function(done) {
        System['import']('output/amd-3.js').then(function(m) {
          return System['import']('amd-3.js');
        })
        .then(function(m) {
          ok(m.p == 5);
          done();
        })
        .catch(done);
      });

      test('AMD rewriting 4', function(done) {
        System['import']('output/amd-4.js').then(function(m) {
          return System['import']('amd-4.js');
        })
        .then(function(m) {
          ok(m.q == 4);
          done();
        })
        .catch(done);
      });

      test('AMD rewriting 5a', function(done) {
        System['import']('output/amd-5a.js').then(function(m) {
          return System['import']('amd-5a.js');
        })
        .then(function(m) {
          ok(m.jquery == 1);
          done();
        })
        .catch(done);
      });

      test('AMD rewriting 5b', function(done) {
        System['import']('output/amd-5b.js').then(function(m) {
          return System['import']('amd-5b.js');
        })
        .then(function(m) {
          ok(m.jquery == 1);
          done();
        })
        .catch(done);
      });

      test('AMD rewriting 6a', function(done) {
        System['import']('output/amd-6a.js').then(function(m) {
          return System['import']('amd-6a.js');
        })
        .then(function(m) {
          ok(m.p != 'a');
          ok(window.p == 'a');
          done();
        })
        .catch(done);
      });

      test('AMD rewriting 6b', function(done) {
        System['import']('output/amd-6b.js').then(function(m) {
          return System['import']('amd-6b.js');
        })
        .then(function(m) {
          ok(m.p == 'b');
          done();
        })
        .catch(done);
      });

      test('Loading UMD', function(done) {
        System['import']('umd.js').then(function(m) {
          ok(m.umd == 'detection');
          done();
        });
      });

      test('AMD 7', function(done) {
        System['import']('output/amd-7.js').then(function() {
          return System['import']('amd-7.js');
        })
        .then(function(m) {
          ok(m.amd == 'object');
          done();
        })
        .catch(done);
      });

      test('AMD 8', function(done) {
        System['import']('output/amd-8.js').then(function() {
          return System['import']('amd-8.js');
        })
        .then(function(m) {
          ok(m == 'dep');
          done();
        })
        .catch(done);
      });

      test('AMD 9', function(done) {
        System['import']('output/amd-9.js').then(function() {
          return System['import']('amd-9.js');
        })
        .then(function(m) {
          ok(m.b.b == 'b');
          ok(m.c == 'c');
          done();
        })
        .catch(done);
      });

      test('AMD 10', function(done) {
        System['import']('output/amd-10.js').then(function() {
          return System['import']('amd-10.js');
        })
        .then(function(m) {
          ok(m.b.b == 'b');
          ok(m.c == 'c');
          done();
        })
        .catch(done);
      });

      test('AMD 11', function(done) {
        System['import']('output/amd-11.js').then(function() {
          return System['import']('amd-11.js');
        })
        .then(function(m) {
          ok(m.b.b == 'b');
          ok(m.c == 'c');
          done();
        })
        .catch(done);
      });

      test('AMD 12', function(done) {
        System['import']('output/amd-12.js').then(function() {
          return System['import']('amd-12.js');
        })
        .then(function(m) {
          ok(m.a == 'a');
          done();
        })
        .catch(done);
      });

      test('CJS Globals', function(done) {
        System['import']('output/cjs-globals.js').then(function() {
          return System['import']('cjs-globals.js');
        })
        .then(function(m) {
          ok(m == 'Buffer');
          done();
        })
        .catch(done);
      });

      test('CJS require.resolve', function(done) {
        System['import']('output/cjs-resolve.js').then(function(m) {
          return System['import']('cjs-resolve.js');
        })
        .then(function(m) {
          ok(m.substr(m.length - 27, 27) == 'fixtures/test-tree/first.js');
          done();
        })
        .catch(done);
      });

      (/PhantomJS/.test(window.navigator.userAgent) ? test.skip : test)('Runtime', function(done) {
        System['import']('output/runtime.js').then(function() {
          return System['import']('runtime.js');
        })
        .then(function(m) {
          ok(m.test);
          done();
        })
        .catch(function(e) {
          // traceur runtime doesn't load in PhantomJS
          if (e.toString().match(/Attempted to assign to readonly/))
            return done();
          done(e);
        });
      });
    });
  </script>
  <script>
    if (window.mochaPhantomJS)
      mochaPhantomJS.run();
    else
      mocha.run();
  </script>
